package com.yutel.google;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import cn.com.xinli.android.service.protocol.EndUserAuth;
import cn.com.xinli.android.service.protocol.EndUserAuth.RegisterRequest;

import com.yutel.security.SecurityUtil;

public class TestEndUserAuth {
	private String AUTHIPADRESS = "192.168.2.243";
	private int connTimeOut = 20000;
	private int dataTimeOut = 60000;

	public static void main(String[] arg) {
		String user = "bb5@bb.com";
		String initPwd = "bbbbbb";
		String pwd = SecurityUtil.encode(initPwd);
		System.out.println("u=" + user + ",p=" + pwd);
		TestEndUserAuth t = new TestEndUserAuth();
//		t.testRegisterRequest(user, pwd);
		t.testLoginRequest(user, pwd);
		// t.testBindRequest(user,pwd,"");
	}

	public void testBindRequest(String user_idString, String user_password,
			String bind_mohoAccount) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		EndUserAuth.BindRequest request = null;
		int response = 1;
		String info = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS, 5246),
					connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			EndUserAuth.BindRequest.Builder builder = EndUserAuth.BindRequest
					.newBuilder();
			builder.setEmail(user_idString);
			builder.setPwd(user_password);
			builder.setMohoAccount(bind_mohoAccount);
			request = builder.build();
			dataOutput.writeInt(request.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(request.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			System.out.println("length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			EndUserAuth.BindResponse bindResponse = EndUserAuth.BindResponse
					.parseFrom(dataini);
			response = bindResponse.getResultCode();
			info = bindResponse.getResultMessage();
			System.out.println("response=" + response + ",info=" + info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testLoginRequest(String user_idString, String user_password) {
		int response = 1;
		EndUserAuth.LoginRequest request = null;
		DataInputStream dataInput = null;
		DataOutputStream dataOutput = null;
		Socket socketClient = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS, 5242),
					connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			EndUserAuth.LoginRequest.Builder builder = EndUserAuth.LoginRequest
					.newBuilder();
			builder.setEmail(user_idString);
			builder.setPwd(user_password);
			request = builder.build();
			dataOutput.writeInt(request.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(request.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			System.out.println("lenth" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			EndUserAuth.LoginResponse loginResponse = EndUserAuth.LoginResponse
					.parseFrom(dataini);
			response = loginResponse.getResultCode();
			if (response == 0) {
				System.out.println("loginResops"
						+ loginResponse.getResultMessage());
			}
			System.out.println("code=" + response + ",Msg"
					+ loginResponse.getResultMessage());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void testRegisterRequest(String user_idString, String user_password) {
		int response = 1;
		RegisterRequest registerRequest = null;
		DataInputStream dataInput = null;
		DataOutputStream dataOutput = null;
		Socket socketClient = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS, 5240),
					connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			EndUserAuth.RegisterRequest.Builder builder = EndUserAuth.RegisterRequest
					.newBuilder();
			builder.setEmail(user_idString);
			builder.setPwd(user_password);
			registerRequest = builder.build();
			dataOutput.writeInt(registerRequest.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(registerRequest.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			System.out.println("lenth" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			EndUserAuth.RegisterResponse registResponse = EndUserAuth.RegisterResponse
					.parseFrom(dataini);
			response = registResponse.getResultCode();
			if (response == 0) {
				System.out.println("ssssssssssss");
			}
			System.out.println("code=" + response + ",Msg"
					+ registResponse.getResultMessage());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
